# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from pymongo import MongoClient


class BossZhipinPipeline(object):

    def __init__(self):
        self.client = MongoClient()
        self.db = self.client['boss_zhipin']
        self.collection = self.db['it']

    def process_item(self, item, spider):
        company = {
            'company_name': item['company_name'],
            'company_list': item['company_list'],
            'company_scale': item['company_scale'],
            'company_brandindustry': item['company_brandindustry'],
            'company_website': item['company_website'],
            'company_jobs': []
        }
        job = {
            'job_name': item['job_name'],
            'job_wages': item['job_wages'],
            'job_place': item['job_place'],
            'job_education': item['job_education'],
            'job_sec': item['job_sec']
        }
        self.collection.update({'company_name': item['company_name']},
                               {'$setOnInsert': company}, True)
        self.collection.update({'company_name': item['company_name']},
                               {'$push': {'company_jobs': job}})
        return item

    def close_spider(self, spider):
        self.client.close()
